package cn.wolfcode.wechat.controller;


import cn.wolfcode.wechat.domain.OrderBill;
import cn.wolfcode.wechat.query.OrderBillQueryObject;
import cn.wolfcode.wechat.service.IOrderBillService;
import cn.wolfcode.wechat.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class OrderBillControllor {
    private String openId;
    @Autowired
    private IOrderBillService billService;


    @RequestMapping("orderBill")
    public String orderBill(Model model, String location) {
        model.addAttribute("location", location);
        return "orderBill";
    }

    @RequestMapping("billsave")
    @ResponseBody
    public JSONResult billsave(OrderBill orderBill) {
        openId = WechatControllor.OPENID;
        if (openId != null) {
            orderBill.setOpenid(openId);
        }
        JSONResult jsonResult = null;
        try {
            billService.creatOrder(orderBill);
            jsonResult = new JSONResult(true, "提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JSONResult(false, e.getMessage());
        }
        return jsonResult;
    }


    @RequestMapping("orderBill_list")
    public String list(Model model, @ModelAttribute("qo") OrderBillQueryObject qo, String username) {
        qo.setKeyword(username);
        openId = WechatControllor.OPENID;
        if (!StringUtils.isEmpty(openId)) {
            qo.setOpenid(openId);
        }
        PageInfo pageInfo = billService.queryPage(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "orderBill_list";
    }

    //后台列表
    @RequestMapping("orderBill_backlist")
    public String list(Model model, @ModelAttribute("qo") OrderBillQueryObject qo ) {
        PageInfo pageInfo = billService.queryPage(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "orderBill_backlist";
    }
}
